﻿using System;
using System.ComponentModel;
using LukasKopenec.LocalizationSupport;
using Notoric.Gui.Windows;

namespace Notoric.Gui.Commands
{
    /// <summary>
    /// Implements a command that set the orientation of an open page to a given value.
    /// </summary>
    /// <remarks>
    /// <para>
    /// The command can execute if a <see cref="PagePanel"/> is selected in the document pane.
    /// </para>
    /// <para>
    /// The execution proceeds as follows:
    /// <list type="number">
    /// <item>
    /// The command calls <see cref="ViewModel.Elements.Page.CreateSetOrientationCommand"/> method on the active page
    /// and executes the created command.
    /// </item>
    /// </list>
    /// </para>
    /// </remarks>
    public class PageOrientationCommand : RibbonCommand
    {
        #region Constructors

        /// <summary>
        /// Creates a new <see cref="PageOrientationCommand"/> instance.
        /// </summary>
        /// <param name="mainWindow">
        /// <para>
        /// Type: <see cref="MainWindow"/>
        /// </para>
        /// <para>
        /// Reference to the application's main window.
        /// </para>
        /// </param>
        /// <param name="orientation">
        /// <para>
        /// Type: <see cref="ViewModel.Elements.PageOrientation"/>
        /// </para>
        /// <para>
        /// The orientation that will be set to the active page.
        /// </para>
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="mainWindow"/> is <see langword="null"/>.
        /// </exception>
        public PageOrientationCommand(MainWindow mainWindow, ViewModel.Elements.PageOrientation orientation)
            : base(mainWindow)
        {
            this.orientation = orientation;

            mainWindow.PropertyChanged += new PropertyChangedEventHandler(mainWindow_PropertyChanged);
            ScreenTipTitle = (string)LocalizationManager.GetValue("Ribbon.CommandTitles.PageAppearance.Orientation");
            ScreenTipText = "Changes the orientation of the current page.";
        }

        #endregion

        #region ICommand Members

        /// <inheritdoc/>
        public override bool CanExecute(object parameter)
        {
            return ApplicationWindow.ActivePage != null;
        }

        /// <inheritdoc/>
        public override void Execute(object parameter)
        {
            var cmd = ApplicationWindow.ActivePage.CreateSetOrientationCommand(orientation);
            cmd.Execute(null);
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Occurs when the value of a property of the <see cref="RibbonCommand.ApplicationWindow"/> has changed.
        /// </summary>
        /// <remarks>
        /// If the modified property is <see cref="MainWindow.ActivePage"/> the handler raises the <see cref="RibbonCommand.CanExecuteChanged"/> event.
        /// </remarks>
        /// <param name="sender">Object that has sent the event</param>
        /// <param name="e">Event data</param>
        private void mainWindow_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ActivePage")
                RaiseCanExecuteChanged();
        }

        #endregion

        #region Private Fields

        /// <summary>
        /// Holds the orientation to be set to the active page.
        /// </summary>
        private ViewModel.Elements.PageOrientation orientation;

        #endregion
    }
}
